<template>
  <scroll @scroll="scroll"
          :listen-scroll="listenScroll"
          :probe-type="probeType"
          :data="data"
          class="listview"
          ref="listview">
    <ul>
      <li class="list-group" v-for="group in data" ref="listGroup">
        <h2 class="list-group-title">{{group.title}}</h2>
        <ul>
          <li class="list-group-item" v-for="item in group.items">
          <!-- 利用懒加载将:src替换成v-lazy -->
            <img v-lazy="item.avatar" class="avatar"/>
            <span class="name">{{item.name}}</span>
          </li>
        </ul>
      </li>
    </ul>
    <!-- .stop.prevent是阻止默认事件和防止冒泡 -->
    <div class="list-shortcut" @touchstart.stop.prevent="onShortcutTouchStart" @touchmove.stop.prevent="onShortcutTouchMove">
      <ul>
        <li 
          class="item" 
          v-for="(item,index) in shortcutList" 
          :data-index="index"
          :class="{'current':currentIndex === index}"
          >
          <!-- 这个class指令的意思是给一个额外的class current给字母一个额外的样式 -->
          {{item}}
        </li>
      </ul> 
    </div>
  </scroll>
</template>

<script type="text/ecmascript-6">
import Scroll from 'base/scroll/scroll'
import { getDate } from 'common/js/dom'
const HEIGHT = 18
  export default {
    created () {
      this.probeType = 3
      this.touch = {},
      this.listenScroll = true,
      this.listHeight = []
    },
    data () {
      return {
        scrollY: -1,
        currentIndex: 0
      }
    },
     props: {
      data: {
        type: Array,
        default: []
      }
    },
    components: {
      Scroll
    },
    //遍历出data中group中的title因为title是一个数组所以用substring截取
    computed: {
      shortcutList () {
        return this.data.map((group) => {
          return group.title.substring(0, 1) 
        })
      }
    },
    methods: {
      onShortcutTouchStart(e) {
        let anchorIndex = getDate(e.target, 'index')//调用js dom中的getdate函数得到index
        let firstTouch = e.touches[0]
        this.touch.y1 = firstTouch.pageY//算出手指触碰的位置到页面上方的高度
        this.touch.anchorIndex = anchorIndex//记录下此时的index值

        this._scrollTo(anchorIndex)
      },
      onShortcutTouchMove(e) {
        let firstTouch = e.touches[0]//获取手指位置
        this.touch.y2 = firstTouch.pageY//获取到页面顶端的高度
        let delta = (this.touch.y2 - this.touch.y1) / HEIGHT | 0
        let anchorIndex = parseInt(this.touch.anchorIndex) + delta//将字符串强转int

        this._scrollTo(anchorIndex)
      },
      scroll (pos) {
        this.scrollY = pos.y
      },
      //新起一个函数将调用封装起来
      _scrollTo (index) {
        if (!index && index === 0) {
          return
        }
        if (index < 0) {
          index = 0
        }else if (index > this.listHeight.length - 2) {
          index = this.listHeight.length
        }
        this.scrollY = -this.listHeight[index]
        this.$refs.listview.scrollToElement(this.$refs.listGroup[index], 0) //第二个参数是一个滚动的动画事假因为没有滚动时间所以设为0
      },
      //计算每一个组的高度
      _calculateHeight () {
        this.listHeight = []
        const list = this.$refs.listGroup
        let height = 0
       this.listHeight.push(height)
       for (let i = 0; i < list.length; i++) {
        let song = list[i]
        height = height + song.clientHeight//height += height + song.clientY
        this.listHeight.push(height)
       }
      }
    },
    //一个监听器去监听data的变化
    watch: {
      data () {
        setTimeout(() => {
          this._calculateHeight()
        },20)
      },
      //监听scrollY在一个区间内就记录下来位置
      scrollY (newY) {
        const listHeight = this.listHeight
        //当滚动到顶部 newY>0
        if (newY > 0) {
          this.currentIndex = 0
          return
        }
        //在中间部分滚动
        for (let i = 0; i < listHeight.length - 1; i++) {
          let height1 = listHeight[i]
          let height2 = listHeight[i + 1]
          if (-newY >= height1 && -newY < height2) {
            this.currentIndex = i
            return 
          }
        }
        //当滚动到底部的时候
        this.currentIndex = listHeight.length - 2
      }
    }
  }
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
  @import "~common/stylus/variable"

  .listview
    position: relative
    width: 100%
    height: 100%
    overflow: hidden
    background: $color-background
    .list-group
      padding-bottom: 30px
      .list-group-title
        height: 30px
        line-height: 30px
        padding-left: 20px
        font-size: $font-size-small
        color: $color-text-l
        background: $color-highlight-background
      .list-group-item
        display: flex
        align-items: center
        padding: 20px 0 0 30px
        .avatar
          width: 50px
          height: 50px
          border-radius: 50%
        .name
          margin-left: 20px
          color: $color-text-l
          font-size: $font-size-medium
    .list-shortcut
      position: absolute
      z-index: 30
      right: 0
      top: 50%
      transform: translateY(-50%)
      width: 20px
      padding: 20px 0
      border-radius: 10px
      text-align: center
      background: $color-background-d
      font-family: Helvetica
      .item
        padding: 3px
        line-height: 1
        color: $color-text-l
        font-size: $font-size-small
        &.current
          color: $color-theme
    .list-fixed
      position: absolute
      top: 0
      left: 0
      width: 100%
      .fixed-title
        height: 30px
        line-height: 30px
        padding-left: 20px
        font-size: $font-size-small
        color: $color-text-l
        background: $color-highlight-background
    .loading-container
      position: absolute
      width: 100%
      top: 50%
      transform: translateY(-50%)
</style>
